<template>
  <div id="app">
    <header class="header">
      <nav>
        <ul>
          <li>
            <a @click="buyProduct">移动商品</a>
          </li>
          <li>
            <a href="#">交话费</a>
          </li>
          <li>
            <a href="#">办套餐</a>
          </li>
          <li>
            <a href="#">我的移动</a>
          </li>
          <li>
            <a href="#">商品订单</a>
          </li>
          <li>
            <a href="#">账单查询</a>
          </li>
        </ul>
      </nav>
    </header>

    <div class="main-content">
      <div class="banner">
        <el-carousel height="450px" type="card">
          <el-carousel-item>
            <img src="../../assets/yyt/llb.jpg" alt />
          </el-carousel-item>
          <el-carousel-item>
            <img src="../../assets/yyt/gwcz.jpg" alt />
          </el-carousel-item>
          <el-carousel-item>
            <img src="../../assets/yyt/kt.jpg" alt />
          </el-carousel-item>
        </el-carousel>
      </div>

      <div class="icon-nav">
        <ul>
          <li>
            <a href="#">
              <i class="fas fa-search"></i> 查询
            </a>
          </li>
          <li>
            <a href="#">
              <i class="fas fa-tasks"></i> 办理
            </a>
          </li>
          <li>
            <a href="#">
              <i class="fas fa-credit-card"></i> 交费
            </a>
          </li>
          <li>
            <a href="#">
              <i class="fas fa-mobile-alt"></i> 号码/套餐
            </a>
          </li>
          <li>
            <a href="#">
              <i class="fas fa-home"></i> 生活
            </a>
          </li>
        </ul>
      </div>

      <div class="sections">
        <div class="section">
          <h2>移动精选</h2>
          <div class="tabs">
            <button @click="hostSks">人气推荐</button>
            <button @click="queryTese">特色专区</button>
          </div>
          <div class="cards" v-if="activeTab === 'popular'">
            <div class="card popular" v-for="item in popularItems" :key="item.id" @click="navigateToDetail(item.itemId)">
              <img :src="require(`@/${item.img}`)" alt class="product-img" width="50" height="60" />
              <h4>{{ item.itemName }} {{ item.color }}</h4>
              <p>售价:{{ item.itemPrice }}元</p>
            </div>
          </div>
          <div class="cards" v-if="activeTab === 'special'">
            <div class="card special" v-for="item in specialItems" :key="item.id" @click="navigateToDetail(item.itemId)">
              <img :src="require(`@/${item.img}`)" alt class="product-img" width="50" height="60" />
              <h4>{{ item.itemName }} {{ item.color }}</h4>
              <p>售价:{{ item.itemPrice }}元</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
  
<script>
export default {
  data() {
    return {
      activeTab: "popular",
      popularItems: [],
      specialItems: [],
      phone: ""
    };
  },
  methods: {
    navigateToDetail(itemId) {
      this.$router.push({ path: "/productDetailOne", query: { "skuId": itemId } });
  },
    buyProduct() {
      this.$router.push("/product");
    },
    hostSks() {
      this.$axios.get("api/productOrder/queryhostSks").then(res => {
        if (res.data.code == 200) {
          this.popularItems = res.data.data;
        }
        this.activeTab = "popular";
      });
    },
    queryTese() {
      this.$axios.get("api/productOrder/queryTese/" + this.phone).then(res => {
        if (res.data.code == 200) {
          this.specialItems = res.data.data;
        }
        this.activeTab = 'special'
      });
      
    }
  },
  created() {
    this.phone = localStorage.getItem("username");
    this.hostSks();
  }
};
</script>
  
<style scoped>


/* 通用样式 */
.card {
  flex: 1 0 calc(25% - 2%); /* 25%为卡片宽度，2%为间隙 */
  background-color: #fff;
  padding: 20px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  text-align: center;
  margin-bottom: 20px;
}

/* “人气推荐”选项卡的样式 */
.cards .popular {
  flex: 1 0 calc(20%);
}

/* “特色专区”选项卡的样式 */
.cards .special {
  flex: 1 0 calc(20% - 2%);
}
.banner .el-carousel img {
  width: 100%;
  height: 100%;
}
.header nav ul {
  list-style-type: none;
  display: flex;
  margin: 0;
  padding: 0;
}
.header nav li {
  margin: 0 10px;
}
.header nav a {
  text-decoration: none;
  color: #333;
  font-weight: bold;
}
.main-content {
  padding: 20px;
}
.banner img {
  width: auto;
  height: auto;
}
.icon-nav {
  display: flex;
  justify-content: space-around;
  margin: 20px 0;
}
.icon-nav ul {
  list-style-type: none;
  display: flex;
  margin: 0;
  padding: 0;
}
.icon-nav li {
  text-align: center;
  margin: 0 15px;
}
.icon-nav a {
  text-decoration: none;
  color: #333;
}
.icon-nav i {
  display: block;
  font-size: 24px;
  margin-bottom: 5px;
}
.sections {
  margin-top: 20px;
}
.section {
  margin-bottom: 40px;
}
.section h2 {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
}
.tabs {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}
.tabs button {
  padding: 10px 20px;
  border: none;
  cursor: pointer;
}
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 2%; /* 设置卡片之间的间距  */
  justify-content: space-between; /* 使卡片可以平均分布在每一行 */
}

.card {
  flex: 1 0 calc(25% - 2%); /* 重新设置宽度以适应每行4个卡片的布局，25% - 2%  */
  background-color: #fff;
  padding: 20px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  text-align: center;
}
.card h3 {
  margin: 10px 0;
}
.card p {
  color: #666;
}
.footer {
  text-align: center;
  padding: 20px 0;
  background-color: #f5f5f5;
}
</style>
  